<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 后台管理员</title>
    <meta name="renderer" content="webkit">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block">
                        <select name="role"  lay-filter="firstCateList">
                            <option value="0">无父级分类</option>
                            @foreach($firstCateList as $cate)
                                <option value="{{$cate['id']}}">{{$cate['name']}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
                <button class="layui-btn layui-btn-warm layuiadmin-btn-admin" data-type="edit">修改</button>
                <button class="layui-btn layui-btn-danger layuiadmin-btn-admin" data-type="del">删除</button>
            </div>

            <table id="table" lay-filter="table">

            </table>

        </div>
    </div>
</div>

<script src="/layuiadmin/layui/layui.js"></script>
<script>
    layui.use(['table','form','element'], function(){
        var table = layui.table;
        var form = layui.form;
        var $ = layui.$;
        //第一个实例
        table.render({
            elem: '#table'
            ,type: "POST"
            ,url: '/admin/cate/getList' //数据接口
            ,page: true //开启分页
            ,limit:50
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: 'ID', width:80, sort: true,}
                ,{field: 'name', title: '名称', minWidth:160}
                ,{field: 'icon', title: 'icon', minWidth:160, sort: true}
                ,{field: 'pidname', title: '父级分类', minWidth:160}
                ,{field: 'status', title: '状态', minWidth: 160}
                ,{field: 'sort', title: '排序', minWidth: 160, sort: true}
                ,{field: 'updated_at', title: '更新时间', minWidth: 160, sort: true}
                ,{field: 'created_at', title: '创建时间', minWidth: 160}
            ]]
        });

        //监听筛选框
        form.on('select(firstCateList)', function(data){
            var field = data.value;
            //执行重载
            table.reload('table', {
                where: {'pid':field}
            });
        });

        var active = {
            del: function(){
                var checkStatus = table.checkStatus('table')
                    ,checkData = checkStatus.data; //得到选中的数据
                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }

                var ids={};
                for (var i=0;i<checkData.length;i++){
                    ids[i]=checkData[i]['id'];
                }
                layer.confirm('确定删除吗？', function(index) {
                    //执行 Ajax 后重载
                    $.ajax({
                        type: "post",
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url: "/admin/cate/del",
                        data: ids,
                        success: function (result) {
                            if (result.code == 1) {
                                layer.msg(result.msg);
                            }
                        }
                    });
                    table.reload('table'); //数据刷新
                    layer.msg('已删除');
                });

            }
            ,edit: function(){
                var checkStatus = table.checkStatus('table')
                    ,checkData = checkStatus.data; //得到选中的数据
                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }

                layer.open({
                    type: 2
                    ,title: '修改分类'
                    ,content: '/admin/cate/edit?id='+checkData[0]['id']
                    ,area: ['450px', '400px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                type: "post",
                                headers: {
                                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                                },
                                url: "/admin/cate/edit?id="+checkData[0]['id'],
                                data: field,
                                success: function (result) {
                                    if (result.code == 1) {
                                        layer.msg(result.msg);
                                    }
                                    layer.close(index1);
                                }
                            });
                            table.reload('table'); //数据刷新
                            layer.close(index); //关闭弹层
                        });

                        submit.trigger('click');
                    }
                });
            }
            ,add: function(){
                layer.open({
                    type: 2
                    ,title: '添加分类'
                    ,content: '/admin/cate/create'
                    ,area: ['450px', '400px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);
                            //监听提交
                            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                var field = data.field; //获取提交的字段
                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    type: "post",
                                    headers: {
                                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                                    },
                                    url: "/admin/cate/create",
                                    data: field,
                                    success: function (result) {
                                        if (result.code == 1) {
                                            layer.msg(result.msg);
                                        }
                                        layer.close(index1);
                                    }
                                });
                                table.reload('table'); //数据刷新
                                layer.close(index); //关闭弹层
                            });

                        submit.trigger('click');
                    }
                });
            }
        }
        //加载事件
        $('.layuiadmin-btn-admin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>

